package com.springboot.learning.project.controller;


import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.springboot.learning.framework.web.controller.BaseController;
import com.springboot.learning.framework.web.http.BaseResponseInfo;
import com.springboot.learning.framework.web.http.ResultErrorCode;
import com.springboot.learning.framework.web.page.PaginationList;
import com.springboot.learning.framework.web.page.PaginationSupport;
import com.springboot.learning.project.entity.po.User;
import com.springboot.learning.project.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

	@Autowired
    private UserService userService;


    /**
     * 添加用户
     */
    @RequestMapping(value = "",method={ RequestMethod.POST })
    public BaseResponseInfo addUser(@Valid User user, BindingResult bindingResult) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
        if (bindingResult.hasErrors()) {
        	info.code=ResultErrorCode.InternalError.getCode();
        	return info;
        }
        userService.addOrUpdateUser(user);
        return info;
    }

    /**
     * 获取用户列表
     */
    @RequestMapping(value = "",method={ RequestMethod.GET })
    public BaseResponseInfo findAllUser(User user) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
    	info.data=userService.selectByCondition(user);
        return info;
    }                           

    /**
     * 获取用户列表
     */
    @RequestMapping(value = "/page",method={ RequestMethod.GET })
    public BaseResponseInfo selectByPage(PaginationSupport ps, User user) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
    	PaginationList pl =userService.selectByPageCondition(ps, user);
    	info.data=pl;
        return info;
    }
   
    /**
     * 查询单个用户
     */
    @RequestMapping(value = "/{id}",method={ RequestMethod.GET })
    public BaseResponseInfo findUserById(@PathVariable("id") Integer id) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
    	//List<User> list =userService.selectList(new EntityWrapper().where("id = {0}", id));
    	info.data=userService.selectUserById(id);
        return info;
    }

    /**
     * 查询单个用户
     */
    @RequestMapping(value = "/account",method={ RequestMethod.POST })
    public BaseResponseInfo findUserByAccout(@RequestParam("account") String account) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
    	//List<User> list =userService.selectList(new EntityWrapper().where("account = {0}", account));
    	info.data=userService.selectUserByAccout(account);
        return info;
    }

    /**
     * 删除用户
     */
    @RequestMapping(value = "/{id}",method={ RequestMethod.DELETE })
    public BaseResponseInfo deleteUserById(@PathVariable("id") Integer id) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
        if (!userService.deleteById(id)) {
        	 info.code=ResultErrorCode.InternalError.getCode();
        }
       
        return info;
    }

}
